package com.tanyin.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanyin.demo.entity.CommonResult;
import com.tanyin.demo.entity.User;
import com.tanyin.demo.mapper.UserMapper;
import com.tanyin.demo.util.JwtUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author tanyin
 * @date 2024-05-19
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    UserMapper userMapper;

    @Override
    public CommonResult login(User user) {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("username",user.getUsername());
        if(userMapper.selectOne(userQueryWrapper)==null){
            return CommonResult.fail(null,"账户不存在！");
        }
        userQueryWrapper.eq("password",user.getPassword());
        User result=userMapper.selectOne(userQueryWrapper);
        if(result==null){
            return CommonResult.fail(null,"密码错误！");
        }
        String token = JwtUtil.create(result.getId(),result.getUsername());
        return CommonResult.success(token);
    }
}
